<template>
  <div class="user-profile">
    <!-- 导航栏 -->

    <input type="file" hidden ref="inputFile" @change="onChange" />
    <van-nav-bar
      class="page-nav-bar"
      title="个人信息"
      left-arrow
      @click-left="$router.back()"
    />
    <!-- /导航栏 -->
    <van-cell @click="$refs.inputFile.click()" title="头像" is-link>
      <van-image class="avatar" fit="cover" round :src="UserInfo.photo" />
    </van-cell>
    <van-cell
      title="昵称"
      @click="isShowEdit = true"
      :value="UserInfo.name"
      is-link
    />
    <van-cell
      title="性别"
      :value="UserInfo.gender === 1 ? '女' : '男'"
      is-link
      @click="isShowEditSex = true"
    />
    <van-cell
      title="生日"
      :value="UserInfo.birthday"
      is-link
      @click="isShowEditBirthday = true"
    />

    <van-popup v-model="isShowEdit" position="bottom" style="height: 100%">
      <user-edit v-model="UserInfo.name" @close="isShowEdit = false" />
    </van-popup>

    <van-popup v-model="isShowEditSex" position="bottom">
      <user-edit-sex @close="isShowEditSex = false" v-model="UserInfo.gender" />
    </van-popup>

    <van-popup v-model="isShowEditBirthday" position="bottom">
      <user-edit-birthday
        v-model="UserInfo.birthday"
        @close="isShowEditBirthday = false"
      />
    </van-popup>

    <van-popup
      v-model="isShowEditAvatar"
      style="height: 100%"
      position="bottom"
    >
      <user-edit-avatar
        v-if="isShowEditAvatar"
        :img="imgUrl"
        @close="isShowEditAvatar = false"
        @update-photo="UserInfo.photo = $event"
      />
    </van-popup>
  </div>
</template>

<script>
import { getUser } from '@/api/user.js'
import UserEdit from './components/UserEdit.vue'
import UserEditSex from './components/UserEditSex.vue'
import UserEditBirthday from './components/UserEditBirthday.vue'
import UserEditAvatar from './components/UserEditAvatar.vue'
export default {
  components: { UserEdit, UserEditSex, UserEditBirthday, UserEditAvatar },
  name: 'User',
  data () {
    return {
      UserInfo: {},
      isShowEdit: false,
      isShowEditSex: false,
      isShowEditBirthday: false,
      isShowEditAvatar: false,
      imgUrl: ''
    }
  },

  created () {
    this.getUser()
  },

  methods: {
    async getUser () {
      try {
        const res = await getUser()
        console.log(res)
        this.UserInfo = res
      } catch (error) {
        console.log(error)
      }
    },
    onChange (e) {
      console.log(e)
      if (e.target.value === '') return
      this.isShowEditAvatar = true
      this.imgUrl = URL.createObjectURL(e.target.files[0])
      e.target.value = ''
    }
  }
}
</script>

<style scoped lang="less">
.user-profile {
  .avatar {
    width: 60px;
    height: 60px;
  }
  /deep/.van-nav-bar__arrow {
    color: #fff;
  }
}
</style>
